//
//  NavView.swift
//  知乎日报Demo
//
//  Created by Ann on 2017/10/18.
//  Copyright © 2017年 Ann. All rights reserved.
//

import UIKit

class NavView: UIView {
    
    var phase:CGFloat = 0{
        didSet{
        self.setNeedsDisplay()

        }
    }
    var isStartAnimate:Bool = false {
        didSet{
            
            if isStartAnimate {
            activityView.startAnimating()
            }else{
                activityView.stopAnimating()
            }
        }
    }
   fileprivate  var bzPath:UIBezierPath?
    
   fileprivate lazy var detailButton:UIButton = {
        let b = UIButton()
        b.setTitle("≣", for: .normal)
        
        b.sizeToFit()
        b.titleShadowColor(for: .normal)
        b.addTarget(self, action: #selector(self.showLeftViewAction), for: UIControlEvents.touchUpInside)
        return b
    }()
    
   fileprivate lazy var todyNewsTitle:UILabel = {
        let l = UILabel()
        l.text = "今日热闻"
    
        l.textColor = UIColor.white
        l.font = UIFont.boldSystemFont(ofSize: 20)
        return l
        
    }()
    
    
   fileprivate lazy var progressView:UIView = {
        let view = UIView()
    
        view.backgroundColor = UIColor.clear
        return view
       
        
    }()
   fileprivate lazy var activityView:UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
    
    
    override func draw(_ rect: CGRect) {
        // Drawing code
        
        let grap = UIGraphicsGetCurrentContext()
        
        
        let point = progressView.center
        let r:CGFloat = 10
        let start = M_PI/2
        let end = CGFloat(M_PI/2)+CGFloat(2 * CGFloat(M_PI) * (1-phase/50))
        
        let bz = UIBezierPath(arcCenter: point, radius: r, startAngle: CGFloat(start), endAngle: end, clockwise: true)
        bz.lineWidth = 4
        UIColor.white.set()
        
        
        grap?.addPath(bz.cgPath)
        grap?.strokePath()
        
    }
 
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        self.backgroundColor = UIColor(red: 60/255, green: 198/255, blue: 253/255, alpha: 1.0)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
  fileprivate  func setupUI(){
        
    
        self.backgroundColor = UIColor.gray
        
        self.addSubview(todyNewsTitle)
        self.addSubview(progressView)
        self.addSubview(detailButton)
        
        layoutSubViews()
        progressView.addSubview(activityView)
    
        
    }
  
   
   fileprivate func layoutSubViews(){
        
        

        todyNewsTitle.snp.makeConstraints { (make) in
        make.center.equalTo(self.snp.center).offset(12)
        }

        progressView.snp.makeConstraints { (make) in
        make.right.equalTo(todyNewsTitle.snp.left).offset(-2)
        make.width.height.equalTo(20)
        make.centerY.equalTo(todyNewsTitle.snp.centerY)

        detailButton.snp.makeConstraints({ (make) in
        make.centerY.equalTo(todyNewsTitle.snp.centerY).offset(-4)
        make.left.equalTo(self.snp.left).offset(8)
        })
        }
        
    }
    
    func showLeftViewAction(){
        
        NotificationCenter.default.post(name:  NSNotification.Name(rawValue: "showUserView"), object: nil)
   
    }
    

}
